﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using ExamAnalysis.Core;
using ExamAnalysis.Model;

namespace ExamAnalysis.BasicInfo
{
    public partial class ExamInfo : System.Web.UI.Page
    {
        static int schoolid = Config.CurrentSchoolID;
        static int calendarid = 0;
        static int[] courseid=Enumerable.Repeat(0,50).ToArray();
        static int examid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                    using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                    {
                        var myCalender = from c in db.SchoolCalender
                                         select c;
                        this.Store2.DataSource = myCalender;
                        this.Store2.DataBind();
                    }
            }
        }

        //protected void Button_Click(object sender, Ext.Net.DirectEventArgs e)
        //{
        //    RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
        //    foreach (SelectedRow row in sm.SelectedRows)
        //    {
        //        schoolid = Convert.ToInt32(row.RecordID);
        //    }
        //    using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
        //    {
        //        var myShool = from c in db.SchoolInfo
        //                      where c.SchoolID == schoolid
        //                      select c.FullName;
        //        foreach (var school in myShool)
        //        {
        //            DropDownField1.Text = school.ToString();
        //        }
        //        var myCalender = from c in db.SchoolCalender
        //                      where c.SchoolID == schoolid
        //                      select c;
        //        this.Store2.DataSource = myCalender;
        //        this.Store2.DataBind();
        //    }
        //    schoolid = 0;
        //}

        protected void Button2_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            DisplayName.Text = "";
            //schoolid = 0;
            this.CalendarWindow.Show();
            Button9.Show();
            Button10.Hide();
            Button13.Hide();
            Button14.Hide();
            ExamName.Hide();
            DropDownField3.Hide();
            DisplayName.Show();
            //DropDownField2.Show();
        }

        protected void Button_2_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            //schoolid = 0;
            ExamName.Text = "";
            DropDownField3.Text = "";
            this.CalendarWindow.Show();
            Button13.Show();
            Button10.Hide();
            Button9.Hide();
            Button14.Hide();
            ExamName.Show();
            DropDownField3.Show();
            DisplayName.Hide();
           // DropDownField2.Hide();
        }

        protected void Button_3_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel3.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    examid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var course = from s in db.CourseInfo
                                 join neweic in db.ExamInCourse on s.CourseID equals neweic.CourseID
                                 join newex in db.ExamInfo on neweic.ExamID equals newex.ExamID
                                 where newex.ExamID == examid
                                 select new { CourseName = s.CourseName, CourseID = s.CourseID, ExamName = newex.ExamName };
                    int i=0;
                    if (course.Count() != 0)
                    {
                        foreach (var row in course)
                        {
                            courseid[i] = row.CourseID;
                            i++;
                        }
                        ExamName.Text = course.First().ExamName;
                        DropDownField3.Text = "";
                        int j = 0;
                        foreach (var row in course)
                        {
                            DropDownField3.Text += row.CourseName + " , ";
                            j++;
                        }
                    }
                }
                this.CalendarWindow.Visible = true;
                this.CalendarWindow.Show();
                Button9.Hide();
                Button10.Hide();
                Button13.Hide();
                Button14.Show();
                ExamName.Show();
                DropDownField3.Show();
                DisplayName.Hide();
               // DropDownField2.Hide();
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个考试安排").Show();
            }
        }

        protected void Button3_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.ClassInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    calendarid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newcalendar = from c in db.SchoolCalender
                                      where c.CalendarID == calendarid
                                      select c;
                    ExamAnalysis.Model.SchoolCalender newc = newcalendar.First();
                    var school = from s in db.SchoolInfo
                                 where s.SchoolID == newc.SchoolID
                                 select s.FullName;
                    //schoolid = newc.SchoolID;
                    DisplayName.Text = newc.DisplayName;
                    //DropDownField2.Text = school.First().ToString();
                }
                this.CalendarWindow.Visible = true;
                this.CalendarWindow.Show();
                Button9.Hide();
                Button10.Show();
                Button13.Hide();
                Button14.Hide();
                ExamName.Hide();
                DropDownField3.Hide();
                DisplayName.Show();
               // DropDownField2.Show();
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个考试安排").Show();
            }
        }

        protected void Button4_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.ClassInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                   calendarid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newcalendar = from c in db.SchoolCalender
                                   where c.CalendarID==calendarid
                                   select c;
                    ExamAnalysis.Model.SchoolCalender newc = newcalendar.First();
                    db.SchoolCalender.DeleteOnSubmit(newc);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", "删除成功").Show();
                    Response.Redirect("ExamInfo.aspx");
                }
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个考试安排").Show();
            }
        }

        protected void Button_4_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel3.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    examid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newexam = from c in db.ExamInfo
                                      where c.ExamID==examid
                                      select c;
                    var neweic = from eic in db.ExamInCourse
                                 where eic.ExamID == examid
                                 select eic;
                    ExamAnalysis.Model.ExamInfo newc = newexam.First();
                    db.ExamInfo.DeleteOnSubmit(newc);
                    db.ExamInCourse.DeleteAllOnSubmit(neweic);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", "删除成功").Show();
                    Response.Redirect("ExamInfo.aspx");
                }
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个考试").Show();
            }
        }

        //protected void Button5_Click(object sender, Ext.Net.DirectEventArgs e)
        //{
        //    RowSelectionModel sm = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
        //    foreach (SelectedRow row in sm.SelectedRows)
        //    {
        //        schoolid = Convert.ToInt32(row.RecordID);
        //    }
        //    using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
        //    {
        //        var myShool = from c in db.SchoolInfo
        //                      where c.SchoolID == schoolid
        //                      select c.FullName;
        //        foreach (var school in myShool)
        //        {
        //            DropDownField2.Text = school.ToString();
        //        }
        //    }
        //}

        protected void Button_5_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            int i = 0;
            DropDownField3.Text = "";
            CheckboxSelectionModel sm = this.GridPanel4.GetSelectionModel() as CheckboxSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                courseid[i] = Convert.ToInt32(row.RecordID);

                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var myCourse = from c in db.CourseInfo
                                   where c.CourseID == courseid[i]
                                   select new { CourseName = c.CourseName };
                    DropDownField3.Text += myCourse.First().CourseName.ToString() + " , ";
                }
                i++;
            }              
        }

        protected void Add_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            //if (DropDownField2.Text.ToString().Equals("请选择"))
            //{
            //    X.Msg.Alert("提示信息", String.Format("请选择一个学校")).Show();
            //}
            //else
            //{
                ExamAnalysis.Model.SchoolCalender newc = new ExamAnalysis.Model.SchoolCalender();
                newc.DisplayName = DisplayName.Text;
                newc.SchoolID = schoolid;
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    db.SchoolCalender.InsertOnSubmit(newc);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", String.Format("成功添加安排")).Show();
                   // schoolid = 0;
                }
                Response.Redirect("ExamInfo.aspx");
            //}
        }
        protected void Add2_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            if (DropDownField3.Text.ToString().Equals("请选择"))
            {
                X.Msg.Alert("提示信息", String.Format("请选择一个课程")).Show();
            }
            else
            {
                ExamAnalysis.Model.ExamInfo newc = new ExamAnalysis.Model.ExamInfo();
                newc.ExamName = ExamName.Text;
                newc.CalendarID = calendarid;
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    db.ExamInfo.InsertOnSubmit(newc);
                    db.SubmitChanges();
                    var newexam=from Exam in db.ExamInfo
                                orderby Exam.ExamID descending
                                select Exam;
                    X.Msg.Alert("提示信息", String.Format("成功添加考试")).Show();
                    for (int i = 0; i < courseid.Count(); i++)
                    {
                        if (courseid[i] != 0)
                        {
                            ExamAnalysis.Model.ExamInCourse neweic = new ExamAnalysis.Model.ExamInCourse();
                            neweic.ExamID = newexam.First().ExamID;
                            neweic.CourseID = courseid[i];
                            db.ExamInCourse.InsertOnSubmit(neweic);
                            db.SubmitChanges();
                        }
                    }
                    courseid = Enumerable.Repeat(0, 50).ToArray();
                }
                Response.Redirect("ExamInfo.aspx");
            }
        }


        protected void Change_Click(object sender, Ext.Net.DirectEventArgs e)
        {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newcalendar = from c in db.SchoolCalender
                                      where c.CalendarID == calendarid
                                      select c;
                    ExamAnalysis.Model.SchoolCalender newc = newcalendar.First();
                    newc.DisplayName = DisplayName.Text;
                    newc.SchoolID = schoolid;
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", String.Format("成功修改信息")).Show();
                    schoolid = 0;
                    calendarid = 0;
                }
                Response.Redirect("ExamInfo.aspx");     
        }
        protected void Change2_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var newexam = from c in db.ExamInfo
                              where c.ExamID == examid
                              select c;
                ExamAnalysis.Model.ExamInfo newc = newexam.First();
                newc.ExamName = ExamName.Text;
                newc.CalendarID = calendarid;
                db.SubmitChanges();
                X.Msg.Alert("提示信息", String.Format("成功修改信息")).Show();
                var neweic = from eic in db.ExamInCourse
                             where eic.ExamID == examid
                             select eic;
                db.ExamInCourse.DeleteAllOnSubmit(neweic);
                db.SubmitChanges();
                CheckboxSelectionModel sm = this.GridPanel4.GetSelectionModel() as CheckboxSelectionModel;
                if (sm.SelectedRows != null)
                {
                    courseid = Enumerable.Repeat(0, 50).ToArray();
                    int i = 0;
                    foreach (SelectedRow row in sm.SelectedRows)
                    {
                        courseid[i] = Convert.ToInt32(row.RecordID);
                        i++;
                    }
                }
                for (int i = 0; i < courseid.Count(); i++)
                {
                    if (courseid[i] != 0)
                    {
                        ExamAnalysis.Model.ExamInCourse neweic2 = new ExamAnalysis.Model.ExamInCourse();
                        neweic2.ExamID = examid;
                        neweic2.CourseID = courseid[i];
                        db.ExamInCourse.InsertOnSubmit(neweic2);
                        db.SubmitChanges();
                    }
                }
                courseid = Enumerable.Repeat(0, 50).ToArray();
                calendarid = 0;
                examid = 0;
            }
            Response.Redirect("ExamInfo.aspx");
        }

        protected void ShowExam_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.ClassInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                   calendarid = Convert.ToInt32(row.RecordID);
                }
             using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var newexam = from ex in db.ExamInfo
                              where ex.CalendarID == calendarid
                              select new { ExamID = ex.ExamID, ExamName = ex.ExamName };
                Store5.DataSource = newexam;
                Store5.DataBind();
                GridPanel3.Show();
             }
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个考试安排").Show();
            }
        }
    }
}